﻿using System;
using Loki.Data;
using Loki.Model;

namespace Luna.Model
{
    public class LunaEntity<TData> : AtomicEntity<TData, Guid>, ILunaEntity, IAtomicEntity<Guid, TData>
         where TData : class, IDataEntity<Guid>
    {
        public LunaEntity()
            : base()
        {
            ID = Guid.NewGuid();
        }

        public LunaEntity(IAtomicEntity<Guid, TData> entity)
            : base(entity)
        {
        }

        public LunaEntity(TData data)
            : base(data)
        {
        }

        public override bool Equals(object other)
        {
            var buffer = other as LunaEntity<TData>;
            return buffer != null && buffer.ID == ID;
        }

        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }

        protected TEntity GetEntity<TEntity, TKey>(IIndexedEnumerable<TEntity, TKey> store, TKey valueID) where TEntity : class
        {
            if (object.Equals(valueID, default(TKey)))
            {
                return null;
            }
            else
            {
                return store[valueID];
            }
        }

        protected TEntity GetEntity<TEntity, TKey>(IIndexedEnumerable<TEntity, TKey> store, TKey? valueID)
            where TEntity : class
            where TKey : struct
        {
            if (valueID.HasValue)
            {
                return store[valueID.Value];
            }
            else
            {
                return null;
            }
        }
    }
}